package com.meilingcloud.biobank.controller;

import com.meilingcloud.biobank.domain.SourceType;
import com.meilingcloud.biobank.service.ISourceTypeService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 样本源类型 Controller
 *
 * @author chenl
 * @date 2021-02-26
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/sourceType")
public class SourceTypeController extends BaseController {

    private final ISourceTypeService iSourceTypeService;

    /**
     * 查询样本源类型 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:sourceType:list')")
    @GetMapping("/list")
    public AjaxResult list(SourceType sourceType) {
        List<SourceType> list = iSourceTypeService.queryListWithForm(sourceType);
        return AjaxResult.success(list);
    }

    /**
     * 获取样本源类型 详细信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:sourceType:query')")
    @GetMapping(value = "/{sourceTypeId}")
    public AjaxResult getInfo(@PathVariable("sourceTypeId") Long sourceTypeId) {
        return AjaxResult.success(iSourceTypeService.queryById(sourceTypeId));
    }

    /**
     * 新增样本源类型
     */
    @PreAuthorize("@ss.hasPermi('biobank:sourceType:add')")
    @Log(title = "样本源类型 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody SourceType sourceType) {
        sourceType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iSourceTypeService.save(sourceType));
    }

    /**
     * 修改样本源类型
     */
    @PreAuthorize("@ss.hasPermi('biobank:sourceType:edit')")
    @Log(title = "样本源类型 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody SourceType sourceType) {
        sourceType.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iSourceTypeService.updateSourceTypeById(sourceType));
    }

    /**
     * 删除样本源类型
     */
    @PreAuthorize("@ss.hasPermi('biobank:sourceType:remove')")
    @Log(title = "样本源类型 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sourceTypeIds}")
    public AjaxResult remove(@PathVariable Long[] sourceTypeIds) {
        return toAjax(iSourceTypeService.removeSourceTypeByIds(Arrays.asList(sourceTypeIds)));
    }
}
